﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVGuide.DataAccess.Common;
using System.Text.RegularExpressions;
using TVGuide.Common;

namespace TVGuide.Parser
{
    class ShantParser : IChannelParser
    {
        public string ChannelName()
        {
            return "Shant";
        }

        public List<Program> ParseProgram()
        {
            throw new NotImplementedException();
        }

        public List<Program> ParseProgram(DateTime date)
        {
            List<Program> tvProgramList = new List<Program>();
            string URL = "http://shanttv.com/am/schedule/";
            Encoding enc = Encoding.UTF8;
            var shantString = HTTPClient.GetSiteContent(URL, enc, this.ChannelName());
            string pattern = "<strong class=\"orange\".*?</td>";
            Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection match = rgx.Matches(shantString);
            int DayOfWeek = 0; // Monday = 0
            string datePattern = @"<strong>\d\d:\d\d</strong>";
            string namePattern = @"</strong>[^<>]*?<br />";          
            foreach (Match item in match) //each item matches for one day in a week started from monday
            {
                Regex dateRgx = new Regex(datePattern, RegexOptions.IgnoreCase);
                MatchCollection dateMatches = dateRgx.Matches(item.Value);
                Regex nameRgx = new Regex(namePattern, RegexOptions.IgnoreCase);
                MatchCollection nameMatches = nameRgx.Matches(item.Value);
                if (dateMatches.Count != nameMatches.Count)
                {
                    break;   // something wrong in parsing
                }
                int _day = date.Day - (int)date.DayOfWeek + 1 + DayOfWeek; //going back to monday
                tvProgramList.AddRange(initProgObjects(dateMatches, nameMatches, date, _day));
                DayOfWeek++;
            }

            return tvProgramList;
        }

        private List<Program> initProgObjects(MatchCollection dateMatches, MatchCollection nameMatches, DateTime date, int day)
        {
            List<Program> programList = new List<Program>();
            int hour;
            int minute;
            int count = 0;
            string name = "";
            string time = "";
            while (count < dateMatches.Count) {

                time = Utils.removeHTMLTags(dateMatches[count].Value);
                Utils.seperateTimeProgramName(time, out hour, out minute);
                name = Utils.removeHTMLTags(nameMatches[count].Value);
                Program tvProgram = new Program();
                tvProgram.Name = name;
                tvProgram.Date = new DateTime(
                    date.Year,
                    date.Month,
                    day,
                    hour,
                    minute,
                    0
                    );
                tvProgram.ChannelId = (int)Enums.Channels.SHANT;
                programList.Add(tvProgram);
                count++;
            }


            return programList;
        }

        public List<DataAccess.Common.Program> ParseProgramForWeek()
        {
            return ParseProgram(DateTime.Today);
        }
    }
}
